<?php

namespace app\admin\controller\deerhome;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\deerhome\Shop;
/**
 * 门店入驻申请管理
 *
 * @icon fa fa-circle-o
 */
class ShopApply extends Backend
{

    /**
     * ShopApply模型对象
     * @var \app\admin\model\deerhome\ShopApply
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\deerhome\ShopApply;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function sh(){
        $id = $this->request->param("id");
        $status = $this->request->param("status");
        if(!in_array($status,[2,3])){
            $this->error("参数错误");
        }
        $data=$this->model->get($id);
        if(!$data){
            $this->error("数据不存在");
        }
        if($data['status']!=1){
            $this->error("已审核过");
        }
        $admin=$this->auth->getUserInfo();
        Db::startTrans();
        try{
            $data->status=$status;
            $data->sh_user=$admin['username'];
            $data->sh_note='';
            $data->sh_time=date("Y-m-d H:i:s");
            $data->save();
            if($status==2){
                $par=collection($data->toArray())->toArray();
                $par['address_face']=$par['province']."/".$par['city']."/".$par['area'];
                $par['status']=1;
                $par['type']=2;
                $par['reg_time']=date("Y-m-d H:i:s");
                $par['open_time']="09:00";
                $par['close_time']="18:00";
                unset($par['id']);
                (new Shop)->allowField(true)->save($par);
            }
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();
    }


}
